class Solution {
    int _target;
    vector<int> path;
    vector<vector<int>> ret;

public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        _target = target;
        dfs(candidates, 0, 0);
        return ret;
    }

    void dfs(vector<int>& candidates, int pos, int sum)
    {
        if(sum == _target)
        {
            ret.push_back(path);
            return;
        }
        if(sum > _target || pos == candidates.size()) // 下面没判断pos这就要判断
            return;

        for (int k = 0; k * candidates[pos] + sum <= _target; ++k) // 枚举个数
        {
            if (k != 0)
                path.push_back(candidates[pos]);
            dfs(candidates, pos + 1, sum + k * candidates[pos]);
        }
        for (int k = 1; k * candidates[pos] + sum <= _target; ++k) // 恢复现场
        {
            path.pop_back();
        }
    }
};